// SubCommodityWarehouseInventoryData.js

import { Util } from '../../utils/util.js'
import { CsDouble } from '../common/csDouble.js'

export class SubCommodityWarehouseInventoryData {
    constructor({
        warehouseId,
        subCommodityId,
        count,
        storeId,
        warehouseName = '',
        storeName = ''
    }) {
        this.warehouseId = warehouseId
        this.subCommodityId = subCommodityId
        this.count = count instanceof CsDouble ? count : new CsDouble(count)
        this.storeId = storeId
        this.warehouseName = warehouseName
        this.storeName = storeName
    }

    static clone(cloneObj) {
        return new SubCommodityWarehouseInventoryData({
            warehouseId: cloneObj.warehouseId,
            subCommodityId: cloneObj.subCommodityId,
            count: cloneObj.count, // 假设 CsDouble 是可共享或不可变的
            storeId: cloneObj.storeId,
            warehouseName: cloneObj.warehouseName,
            storeName: cloneObj.storeName,
        })
    }

    static fromJson(json) {
        return new SubCommodityWarehouseInventoryData({
            subCommodityId: Util.jsonToString(json.specId),
            warehouseId: Util.jsonToString(json.storageId),
            warehouseName: Util.jsonToString(json.storageName),
            storeId: Util.jsonToString(json.storeId),
            storeName: Util.jsonToString(json.storeName),
            count: Util.jsonToCsDouble(json.count, 0),
        })
    }

    // 对象传递拷贝 与  fromJson 一一对应
    toCopyJson() {
        return {
            specId: this.subCommodityId,
            storageId: this.warehouseId,
            storageName: this.warehouseName,
            storeId: this.storeId,
            storeName: this.storeName,
            count: this.count.value,
        }
    }
}
